<?php

class CashMovementsController extends AppController {

    var $name = 'CashMovements';
    var $uses = array('CashMovement', 'Cash');
    
    function beforeFilter() {
        parent::beforeFilter();
        // Limito que si es un usuario del tipo user no pueda acceder
        $accion = $this->action;
        if ($this->Auth->user('group_id') != GROUP_ADMIN && !in_array($accion, array('view'))) {
            $this->Session->setFlash(__('Acceso Restringido', true));
            $this->redirect(array('controller' => 'futures', 'action' => 'index'));
        }
    }

    /*
      function index() {
      $this->CashMovement->recursive = 0;
      $this->set('cashMovements', $this->paginate());
      }
     */

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Movimiento inválido', true));
            $this->redirect(array('controller' => 'cashes', 'action' => 'view'));
        }
        $this->set('cashMovement', $this->CashMovement->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {

            //si es un movimiento de salida
            if ($this->data['CashMovement']['type'] == 'out') {
                if (!$this->Cash->controlarPago($this->data['CashMovement']['amount'])) {
                    //si no alcanza la plata para pagar esto
                    $this->Session->setFlash(__('No hay dinero suficiente en la caja para retirar ese monto', true));
                    $this->redirect($this->referer());
                }
            }

            $this->CashMovement->create();
            if ($this->CashMovement->save($this->data)) {
                $this->Session->setFlash(__('El movimiento ha sido guardado.', true));
                $this->redirect(array('controller' => 'cashes', 'action' => 'view'));
            } else {
                $this->Session->setFlash(__('El movimiento no ha sido guardado. Por favor, intente nuevamente.', true));
            }
        }
    }

    /*
      function edit($id = null) {
      if (!$id && empty($this->data)) {
      $this->Session->setFlash(__('Invalid cash movement', true));
      $this->redirect(array('action' => 'index'));
      }
      if (!empty($this->data)) {
      if ($this->CashMovement->save($this->data['CashMovement'])) {
      $this->Session->setFlash(__('The cash movement has been saved', true));
      $this->redirect(array('action' => 'index'));
      } else {
      $this->Session->setFlash(__('The cash movement could not be saved. Please, try again.', true));
      }
      }
      if (empty($this->data)) {
      $this->data = $this->CashMovement->read(null, $id);
      }
      $cashes = $this->CashMovement->Cash->find('list');
      $users = $this->CashMovement->User->find('list');
      $jobs = $this->CashMovement->Job->find('list');
      $employees = $this->CashMovement->Employee->find('list');
      $providers = $this->CashMovement->Provider->find('list');
      $expenses = $this->CashMovement->Expense->find('list');
      $this->set(compact('cashes', 'users', 'jobs', 'employees', 'providers', 'expenses'));
      }

      function delete($id = null) {
      if (!$id) {
      $this->Session->setFlash(__('Invalid id for cash movement', true));
      $this->redirect(array('action' => 'index'));
      }
      if ($this->CashMovement->delete($id)) {
      $this->Session->setFlash(__('Cash movement deleted', true));
      $this->redirect(array('action' => 'index'));
      }
      $this->Session->setFlash(__('Cash movement was not deleted', true));
      $this->redirect(array('action' => 'index'));
      }
     * 
     */
}
